﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
//using Microsoft.Office.Interop.Excel;
using DevExpress.Spreadsheet.Functions;
using DevExpress.Spreadsheet;
using System.IO;
using COMOSOFT.Class;
//using DevExpress.XtraSpreadsheet.Model;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmPreview : Form
    {
        List<string> ListItem = new List<string>();
        ClassShare cls = new ClassShare("FrmPreview");
        GridView GV = null;
        public FrmPreview(GridView _GV, List<string> _List)
        {
            InitializeComponent();            
            this.GV = _GV;
            this.ListItem = _List;
        }
        public void LoadData()
        {
            try
            {
                MemoryStream sstream = new MemoryStream();
                DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
                xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
                xlsOption.ShowGridLines = false;
                xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;

                GV.ExportToXls(sstream, xlsOption);
                spreadsheet_TrimList.LoadDocument(sstream, DocumentFormat.Xls);
                spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(1, 100);
                //spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Rows.AutoFit(1, 100);
                spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Rows[17].Alignment.WrapText = true;
                for (int i = 0; i < 15; i++)
                {
                    spreadsheet_TrimList.ActiveWorksheet.Rows.Insert(i, 1);
                }
                if (!File.Exists(ClassShare.lcSystem + "logo.PNG"))
                    cls.Dowload(ClassShare.ftpSystem, ClassShare.lcSystem, "logo.PNG");
                spreadsheet_TrimList.ActiveWorksheet.Shapes.AddPicture(ClassShare.lcSystem + "logo.PNG", 50, 50, 800, 300, false);
                spreadsheet_TrimList.ActiveWorksheet.Rows[6][9].SetValue("TRIM LIST DETAILS");
                spreadsheet_TrimList.ActiveWorksheet.Rows[6][9].Font.UnderlineType = UnderlineType.Single;
                spreadsheet_TrimList.ActiveWorksheet.Rows[6][9].Font.Bold = true;
                spreadsheet_TrimList.ActiveWorksheet.Rows[6][9].Font.Color = Color.Black;
                spreadsheet_TrimList.ActiveWorksheet.Rows[6][9].Font.Size = 18;
                spreadsheet_TrimList.ActiveWorksheet.Rows[7][9].SetValue("          Export Date: " + DateTime.Now.ToShortDateString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[7][9].Font.Bold = true;
                //spreadsheet_TrimList.ActiveWorksheet.Range["H7:N7"].FillColor = Color.LightGray;
                spreadsheet_TrimList.ActiveWorksheet.Name = "TrimList_" + ListItem[2].ToString();
                spreadsheet_TrimList.ActiveWorksheet.Rows[9][1].SetValue("Trim Name: " + ListItem[0].ToString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[10][1].SetValue("Customer: " + ListItem[1].ToString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[11][1].SetValue("Style No: " + ListItem[2].ToString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[9][10].SetValue("Customer PO: " + ListItem[3].ToString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[10][10].SetValue("Trim PO: " + ListItem[4].ToString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[11][10].SetValue("Issue Date: " + Convert.ToDateTime(ListItem[5]).ToShortDateString());
                spreadsheet_TrimList.ActiveWorksheet.Rows[8][0].SetValue("1/Master Info:");
                spreadsheet_TrimList.ActiveWorksheet.Rows[8][0].Font.Size=10;
                spreadsheet_TrimList.ActiveWorksheet.Rows[8][0].Font.Color=Color.Red;
                spreadsheet_TrimList.ActiveWorksheet.Rows[8][0].Font.Bold = true;
                spreadsheet_TrimList.ActiveWorksheet.Rows[8][0].Font.Italic = true;
                spreadsheet_TrimList.ActiveWorksheet.Rows[14][0].SetValue("2/Details Info:");
                spreadsheet_TrimList.ActiveWorksheet.Rows[14][0].Font.Size = 10;
                spreadsheet_TrimList.ActiveWorksheet.Rows[14][0].Font.Bold = true;
                spreadsheet_TrimList.ActiveWorksheet.Rows[14][0].Font.Italic = true;
                spreadsheet_TrimList.ActiveWorksheet.Rows[14][0].Font.Color = Color.Red;

                spreadsheet_TrimList.ActiveWorksheet.Range["B8:L12"].Font.Size = 10;
                spreadsheet_TrimList.ActiveWorksheet.Range["B8:L12"].Font.Color = Color.Black;
                spreadsheet_TrimList.ActiveWorksheet.Range["B8:L12"].Font.Bold = true;
                spreadsheet_TrimList.ActiveWorksheet.Range["B8:L12"].Font.Italic = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         
        }

        private void dataNavigator_TrimList_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Print")
                {
                    spreadsheet_TrimList.ShowPrintDialog();
                }
                else
                {
                    String fileName = null;
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                    sfd.Title = "Save an Excel File";
                    sfd.FileName = "TrimList_" + ListItem[0].ToString() + String.Format("{0:MMMddyyyy}", DateTime.Now);
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            fileName = sfd.FileName;
                            spreadsheet_TrimList.SaveDocument(fileName, DocumentFormat.Xls);
                            DialogResult dlr = MessageBox.Show("Open File Export", "Do you want to open it?",
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dlr == DialogResult.Yes)
                            {
                                System.Diagnostics.Process.Start(fileName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
